﻿using System.Text.RegularExpressions;
using System.Threading.Tasks.Dataflow;
using Contract;
using Utility;

namespace Service
{
    public class Source: ISource
    {
        private const string iniFilePath = "D:\\TopData\\Config" + "\\code.ini";

        IniFile IniFile = new IniFile(iniFilePath);

        public IEnumerable<DirectiveInfo> GetDirectiveInfos()
        {
            List<DirectiveInfo> directiveInfos = new();

            // 一级指令

            var baseDirectiveNames = IniFile.GetSectionNames();
            foreach (var baseDirectiveName in baseDirectiveNames)
            {
                // 添加一级指令
                var baseDirective = new DirectiveInfo(baseDirectiveName, baseDirectiveName, "", default);
                directiveInfos.Add(baseDirective);

                // 二级指令
                var secondDirectiveNames = IniFile.GetSectionValues(baseDirectiveName);
                // 添加二级指令
                foreach (var secondDirectiveName in secondDirectiveNames)
                {
                    //var secondDirective = new DirectiveInfo(secondDirectiveName.Value, secondDirectiveName.Key,
                    //    baseDirectiveName, default);
                    // 添加参数
                    //secondDirective.ParameterInfos = ?
                    // 二级指令才有参数
                    ParameterInfo[] ParameterInfos = default;
                    List<ParameterInfo> ParameterInfoList = new();
                    var functionExpression = secondDirectiveName.Value;

                    string pattern = @"\(([^)]*)\)"; // 正则表达式模式，匹配括号内的内容
                    MatchCollection matches = Regex.Matches(functionExpression, pattern);
                    foreach (Match match in matches)
                    {
                        Console.WriteLine(match.Groups[1].Value); // 输出括号内的内容
                        ParameterInfo parameterInfo = new(match.Groups[1].Value, match.Groups[1].Value, typeof(string));
                        ParameterInfoList.Add(parameterInfo);
                    }

                    ParameterInfos = ParameterInfoList.ToArray();
                    var secondDirective = new DirectiveInfo(secondDirectiveName.Value, secondDirectiveName.Key,
                        baseDirectiveName, ParameterInfos);

                    directiveInfos.Add(secondDirective);
                }

            }

            return directiveInfos;
        }

        public IEnumerable<VariableInfo> GetVariableInfos()
        {
            List<VariableInfo> VariableInfos = new List<VariableInfo>();
            for (int i = 0; i < 100; i++)
            {
                VariableInfo variableInfo = new VariableInfo(i.ToString(), i.ToString());
                VariableInfos.Add(variableInfo);
            }

            return VariableInfos;
        }
    }
}
